//
//  CollegeViewController.m
//  meiyaya
//
//  Created by 张领瑞 on 2017/9/11.
//  Copyright © 2017年 ShiFeiEr. All rights reserved.
//

#import "CollegeViewController.h"
#import "PrefixHeader.pch"


@interface CollegeViewController ()<ZLRSegmentControlDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,HeaderRightBtnDelegate,LectureViewCellFocusDelegate>

@property (nonatomic,strong)ZLRSegmentControl *seg;//选择器
@property (nonatomic,strong)UICollectionView *collectionView;//collectionview
@property (nonatomic,strong) NSMutableArray *dataSourceArray;
@property (nonatomic,assign)showState showState;//显示状态，展示的页面

@end

@implementation CollegeViewController
#pragma mark 懒加载
-(NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [[NSMutableArray alloc] init];
    }
    return _dataSourceArray;
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsVerticalScrollIndicator = NO;//隐藏滑杆
        
    }
    return _collectionView;
}

#pragma mark ZLRSegmentControlDelegate Method
-(void)didSelectedAtIndext:(NSInteger)index{
    NSLog(@"%d",[_seg currentSelectedAtIndex]);
    switch (index) { //网络请求刷新数据
        case 0:
            self.showState = showCourse;
            self.collectionView.backgroundColor = rgb_Hex(0xFFFFFF, 1);
            break;
        case 1:
            self.showState = showLecture;
            self.collectionView.backgroundColor = rgb_Hex(0xFFFFFF, 1);
            break;
        case 2:
            self.showState = showInfo;
            self.collectionView.backgroundColor = rgb_Hex(0xFFFFFF, 1);
            break;
        case 3:
            self.showState = showActive;
            self.collectionView.backgroundColor = rgb_Hex(0xF3F3F3, 1);
            break;
        default:
            break;
    }
    [self.collectionView reloadData];
}
#pragma mark HeaderRightBtnDelegate Method
//课程列表跳转更多页面
-(void)rightMoreBtn:(UIButton *)sender{
    
    VideoListViewController *videoListVC = [[VideoListViewController alloc] init];
    switch (sender.tag) {
        case 100:    //跳转到正在直播更多页面
            videoListVC.showVideo = VideoShowAliving;
            break;
        case 101:   //跳转到精彩回放更多页面
            videoListVC.showVideo = VideoShowHotReplay;
            break;
        default:
            break;
    }
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:videoListVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
#pragma mark LectureViewCellFocusDelegate Method
//讲师关注按钮
-(void)lectureFocusDidSelected:(UIButton *)sender{
    if (sender.selected == YES) {
        sender.selected = NO;
    }else{
        sender.selected = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置展示状态
    self.showState = showCourse;
    //添加控件
    [self setUpUI];
    //添加手势
    [self setUpGesture];
    //注册单元格
    [ _collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CollectionViewCell"];
    [ _collectionView registerNib:[UINib nibWithNibName:@"LectureViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"LectureViewCell"];
    [ _collectionView registerNib:[UINib nibWithNibName:@"BznsInfoCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"BznsInfoCollectionViewCell"];
    [ _collectionView registerNib:[UINib nibWithNibName:@"ActiveViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ActiveViewCell"];
    [ _collectionView registerNib:[UINib nibWithNibName:@"HotReplayViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HotReplayViewCell"];
    //注册区头
    [ _collectionView registerNib:[UINib nibWithNibName:@"HeaderReusableView" bundle:[NSBundle mainBundle]]  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderReusableView"];
    //注册区尾
    [ _collectionView registerNib:[UINib nibWithNibName:@"FooterReusableView" bundle:[NSBundle mainBundle]]  forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterReusableView"];
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//添加控件ZLRSegmentControl和UICollectionView
-(void)setUpUI{
    _seg = [[ZLRSegmentControl alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 48) titileArray:@[@"课程",@"讲师",@"资料",@"活动"]];
    _seg.backgroundColor = [UIColor whiteColor];
    _seg.delegate = self;
    [self.view addSubview:_seg];
    
    [self.view addSubview:self.collectionView];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_seg.mas_bottom).offset(10);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
   
    
}
#pragma mark  添加swipe手势 及 响应
-(void)setUpGesture{
    UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeGesture:)];
    UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeGesture:)];
    leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.collectionView addGestureRecognizer:leftGesture];
    [self.collectionView addGestureRecognizer:rightGesture];
}

-(void)leftSwipeGesture:(UISwipeGestureRecognizer*)sender{//左滑手势响应
    
    int currentIndex = [_seg currentSelectedAtIndex];
    if (currentIndex == 3) {
        [_seg setSelectedAtIndex:0];
        [self didSelectedAtIndext:0]; //手势加入数据加载响应
    }else{
        [_seg setSelectedAtIndex:currentIndex+1];
        [self didSelectedAtIndext:currentIndex+1]; //手势加入数据加载响应
    }
}
-(void)rightSwipeGesture:(UISwipeGestureRecognizer*)sender{ //右滑手势响应
    
    int currentIndex = [_seg currentSelectedAtIndex];
    if (currentIndex == 0) {
        [_seg setSelectedAtIndex:3];
        [self didSelectedAtIndext:3];//手势加入数据加载响应
    }else{
        [_seg setSelectedAtIndex:currentIndex-1];
        [self didSelectedAtIndext:currentIndex-1];//手势加入数据加载响应
    }
    
}

#pragma mark UICollectionViewDataSource Method
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (self.showState == showCourse) {//课程有争执直播与精彩回放两个section
        return 2;
    }else{
        return 1;
    }
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   // return self.dataSourceArray.count;
    return 4;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell;
    if (self.showState == showCourse) {
        if (indexPath.section == 0) {
            CollectionViewCell *aliveCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
            cell = aliveCell;
        }
        if (indexPath.section == 1) {
            HotReplayViewCell * replayCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotReplayViewCell" forIndexPath:indexPath];
            cell = replayCell;
        }
        
    }else if (self.showState == showLecture){
        LectureViewCell *lectureCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LectureViewCell" forIndexPath:indexPath];
        lectureCell.delegate = self;
        cell = lectureCell;
    }else if (self.showState == showInfo){
        BznsInfoCollectionViewCell *infoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BznsInfoCollectionViewCell" forIndexPath:indexPath];
        cell = infoCell;
        
    }else if(self.showState == showActive){
        ActiveViewCell *activeCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ActiveViewCell" forIndexPath:indexPath];
        cell = activeCell;
        
    }
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}
#pragma mark  UICollectionViewDelegateLayout Method
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (self.showState == showLecture) {
        return UIEdgeInsetsMake(0, 10, 10, 10);
    }
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.showState == showCourse) {
        return ITEM_SIZE;
    }else if (self.showState == showLecture) {
        return ITEM_LECTURE_SIZE;
    }else if (self.showState == showInfo) {
        return CGSizeMake(SCREENWIDTH, 100);
    }else if(self.showState == showActive){
        return CGSizeMake(SCREENWIDTH-20, 140);
    }else{
      return CGSizeMake(SCREENWIDTH, 0);
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (self.showState ==showCourse) {
        return CGSizeMake(SCREENWIDTH, 26);//课程
    }else{
        return CGSizeMake(SCREENWIDTH, 0); //其他
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (self.showState ==showCourse) {
        return CGSizeMake(SCREENWIDTH, 10);//课程
    }else{
        return CGSizeMake(SCREENWIDTH, 0); //其他
    }
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableview;
    if (self.showState == showCourse) {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
           HeaderReusableView* view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderReusableView" forIndexPath:indexPath];
            if (indexPath.section == 0) {
                view.sectionTitleLabel.text = @"正在直播";
                view.rightMoreBtn.tag = 100;
            }
            if (indexPath.section == 1) {
                view.sectionTitleLabel.text = @"精彩回放";
                view.rightMoreBtn.tag = 101;
            }
            view.delegate = self;
            reusableview = view;
        }
    }
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        FooterReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterReusableView" forIndexPath:indexPath];
        reusableview = view;
    }
    return reusableview;
}
- (IBAction)jumpToSearchController:(id)sender {
    
    SearchViewController *searchVC = (SearchViewController *)[self viewControllerInMStoryboardWithName:@"Main" WithIdentifier:@"SearchViewController"];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:NO];
    self.hidesBottomBarWhenPushed = NO;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
